  <template>
    <div class="login-container">
      <el-form ref="form" :rules="rules" :model="form" label-width="80px" class="login-form">
        <h2 class="login-title">管理系统</h2>
        <el-form-item label="邮箱" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" show-password></el-input>
          <router-link to="/login/password/reset">
            忘记密码
          </router-link>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">登录</el-button>
          <el-button type="primary" @click="register()">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>
<script>
import { RSAEncrypt } from '@/api/password/password'
import { login } from '@/api/request/user'
// import store from '@/store/store'
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: '用户名不能为空', trigger: 'blur'},
          {min: 3, max: 50, message: '用户名3-5位', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '密码不能为空', trigger: 'blur'},
          {min: 3, max: 15, message: '密码3-5位', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(valid => {
        // console.log(valid) 验证通过为true，有一个不通过就是false
        if (valid) {
          const user = this.form.username
          const enpas = RSAEncrypt(this.form.password)
          console.log(enpas)
          login({
            email: user,
            password: enpas
          }).then(data => {
            console.log(data)
            this.$store.commit('login', {token: data.token, user: user})
            this.$router.replace({path: '/dashboard'})
          })
          // var jres = JSON.parse(res.data)
          // if (res.code === 0) {
          // console.log(res)
          // }
          console.log(enpas)
          console.log(user)
          // console.log(store.token)
          // if ( res['code'] === 0 )
          // {
          //   res['data']
          // }
          // 通过的逻辑
        } else {
          console.log('验证失败')
          return false
        }
      })
    },
    register () {
      this.$router.replace({path: '/register'})
    },
    resetPassword () {
      console.log('验证失败')
    }
  }
}
</script>

<style acoped>
  .login-form {
    width: 350px;
    margin: 160px auto; /* 上下间距160px，左右自动居中*/
    background-color: rgb(255, 255, 255, 0.8); /* 透明背景色 */
    padding: 30px;
    border-radius: 20px; /* 圆角 */
  }

  /* 背景 */
  .login-container {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: rgba(105, 148, 212, 0.8);
  }

  /* 标题 */
  .login-title {
    color: #f39810;
    text-align: center;
  }
</style>
